<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class manages all about players.
 *
 * @package Aurorum
 * @subpackage Controllers
 */

class ADS_SettingsController extends ADS_DefaultController{
    
    const STATUS_SETTINGS_EDIT_OK = 1;
    
    static $RIGHTS_DATA_SOURCE = array(
        'C', 'R', 'U', 'D'
    );
    
    public function handleEditSettings(){
        
        update_option('ads_player_monitor_period', (int) ADS_Input::postVar(
            'ads_player_monitor_period', 10
        ));
        update_option('ads_player_offline_timeout', (int) ADS_Input::postVar(
            'ads_player_offline_timeout', 300
        ));
        //redir
        ADS_Navigator::redir('self:defaultAction',array(
            'msg' => self::STATUS_SETTINGS_EDIT_OK
        ));
        
    }
    
    public function handleDefaultAction(){
        //add variables to output
        ADS_Output::register('settings', array(
            'ads_player_monitor_period'  => get_option('ads_player_monitor_period', 10),
            'ads_player_offline_timeout' => get_option('ads_player_offline_timeout', 300)
        ));
    }
    
    public function handleUpdateUserSettings(){
        
        $urModel = new ADS_UserRightsModel();
        
        //get variables
        $userId = (int) ADS_Input::postVar('userId', 0);
        $userRights = (array) ADS_Input::postVar('hasRight', array());
        
        $urModel->removeAllRights($userId);
        foreach($userRights as $assetId => $userRight){
            foreach ($userRight as $value) {
                if(in_array($value, self::$RIGHTS_DATA_SOURCE)){
                    $urModel->addUserRight($userId, $assetId, $value);
                }
            }            
        }        
        //redir
        ADS_Navigator::redir('self:userSettings',array(
            'msg' => self::STATUS_SETTINGS_EDIT_OK
        ));
    }
    
    public function handleUserSettings(){
        
        $dsModel = new ADS_DataSourceModel();
        $urModel = new ADS_UserRightsModel();
        
        $users = get_users('role=author');
        $userId = (int)ADS_Input::getVar('userId', $users[0]->ID);
        //add variables to output
        ADS_Output::register(
            'sources',
            $dsModel->getSources()
        );
        $userRights = array();
        foreach($urModel->getUserRights($userId) as $userRight){
            $userRights[$userRight->assetId][$userRight->user_right] = TRUE;
        }
        //ADS_Debug::dump($userRights);
        ADS_Output::register(
            'userRights',
            $userRights
        );
        ADS_Output::register('users', $users);
        ADS_Output::register('userId', $userId);
    }   
          
}